Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@turf/center-of-mass
Advanced tools
@turf/center-of-mass is a module from the Turf.js library that calculates the center of mass (centroid) of a given GeoJSON feature. This is particularly useful for geographic data analysis and visualization, where determining the central point of a polygon or multi-polygon is required.
Calculate Center of Mass for a Polygon
This feature calculates the center of mass for a given polygon. The code sample demonstrates how to create a polygon and then use the @turf/center-of-mass module to find its centroid.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-70.603637, -33.399918],
[-70.614624, -33.395332],
[-70.639343, -33.392466],
[-70.659942, -33.394759],
[-70.683975, -33.404504],
[-70.697021, -33.419406],
[-70.701141, -33.434306],
[-70.700454, -33.446339],
[-70.694274, -33.458369],
[-70.682601, -33.465816],
[-70.668869, -33.472117],
[-70.646209, -33.473835],
[-70.624923, -33.472117],
[-70.609817, -33.468107],
[-70.595397, -33.458369],
[-70.587158, -33.442901],
[-70.587158, -33.426283],
[-70.590591, -33.414248],
[-70.594711, -33.406224],
[-70.603637, -33.399918]
]]);
const centerOfMass = turf.centerOfMass(polygon);
console.log(centerOfMass);
Calculate Center of Mass for a MultiPolygon
This feature calculates the center of mass for a given multi-polygon. The code sample demonstrates how to create a multi-polygon and then use the @turf/center-of-mass module to find its centroid.
const turf = require('@turf/turf');
const multiPolygon = turf.multiPolygon([
[[
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681]
]],
[[
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681]
]]
]);
const centerOfMass = turf.centerOfMass(multiPolygon);
console.log(centerOfMass);
Geolib is a library to provide basic geospatial operations like distance calculation, bounding boxes, and more. It can calculate the centroid of a set of points, but it does not specifically focus on the center of mass for polygons or multi-polygons like @turf/center-of-mass.
JSTS is a JavaScript library of spatial predicates and functions for processing geometry. It includes functions for calculating centroids, but it is more complex and offers a broader range of spatial operations compared to the focused functionality of @turf/center-of-mass.
Takes a feature or a featureCollection and returns its center of mass using this formula: Centroid of Polygon.
Parameters
fc
(FeatureCollection | Feature) the feature collection or featureExamples
var feature = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
4.854240417480469,
45.77258200374433
],
[
4.8445844650268555,
45.777431068484894
],
[
4.845442771911621,
45.778658234059755
],
[
4.845914840698242,
45.779376562352425
],
[
4.846644401550292,
45.78021460033108
],
[
4.847245216369629,
45.78078326178593
],
[
4.848060607910156,
45.78138184652523
],
[
4.8487043380737305,
45.78186070968964
],
[
4.849562644958495,
45.78248921135124
],
[
4.850893020629883,
45.78302792142197
],
[
4.852008819580077,
45.78374619341895
],
[
4.852995872497559,
45.784075398324866
],
[
4.853854179382324,
45.78443452873236
],
[
4.8549699783325195,
45.78470387501975
],
[
4.85569953918457,
45.784793656826345
],
[
4.857330322265624,
45.784853511283764
],
[
4.858231544494629,
45.78494329284938
],
[
4.859304428100585,
45.784883438488365
],
[
4.858360290527344,
45.77294120818474
],
[
4.854240417480469,
45.77258200374433
]
]
]
}
};
var centerOfMass = turf.centerOfMass(feature);
//=centerOfMass
Returns Feature<Point> the center of mass
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this module individually:
$ npm install @turf/center-of-mass
Or install the Turf module that includes it as a function:
$ npm install @turf/turf
FAQs
turf center-of-mass module
The npm package @turf/center-of-mass receives a total of 789,456 weekly downloads. As such, @turf/center-of-mass popularity was classified as popular.
We found that @turf/center-of-mass demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.